//
//  KATBubbleWindow.m
//  KATFramework
//
//  Created by Kantice on 15/11/26.
//  Copyright © 2015年 KatApp. All rights reserved.
//

#import "KATBubbleWindow.h"

@implementation KATBubbleWindow

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


+ (instancetype)bubbleWindow
{
    KATBubbleWindow *window=[[[self alloc] init] autorelease];
        
    [window initData];
    
    return window;
}


//初始化数据
- (void)initData
{
    self.screenSize=[[UIScreen mainScreen] bounds].size;
    self.frame=CGRectMake(_screenSize.width, 0, _screenSize.width, _screenSize.height);//开始隐藏
    self.contentFrame=CGRectMake(_screenSize.width*0.1, _screenSize.height*0.2, _screenSize.width*0.8, _screenSize.height*0.5);//默认尺寸
    self.backgroundColor=[UIColor clearColor];
    self.cornerRadius=6.0f;
    self.bgColor=KCOLOR_BACKGROUND;
    self.borderColor=KCOLOR_BACKGROUND;
    self.shadowColor=COLOR(COLOR_G0-96);
    self.direction=BW_DIRECTION_NONE;
    self.animationDuration=0.4f;
    self.arrowPoint=CGPointMake(0, 0);
    self.borderWidth=0.0f;
}


//初始化
- (void)initWindow
{
    //阴影层
    self.shadow=[[[UIView alloc] init] autorelease];
    _shadow.frame=CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    _shadow.backgroundColor=_shadowColor;
    [self addSubview:_shadow];
    
    //阴影层点击关闭
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
    [_shadow addGestureRecognizer:tap];
    [tap release];
    
    
    //气泡层
    self.bubble=[CAShapeLayer layer];
    _bubble.strokeColor=_borderColor.CGColor;
    _bubble.lineWidth=_borderWidth;
    _bubble.fillColor=_bgColor.CGColor;
    _bubble.lineJoin=kCALineJoinRound;
    
    UIBezierPath *path=[[UIBezierPath alloc] init];//路径
    
    if(_direction==BW_DIRECTION_UP)//上箭头
    {
        float h=_contentFrame.origin.y-_arrowPoint.y;//箭头三角形高度
        float w2=h*0.5;//箭头三角形半宽
        
        [path addArcWithCenter:CGPointMake(_contentFrame.origin.x+_cornerRadius, _contentFrame.origin.y+_cornerRadius) radius:_cornerRadius startAngle:M_PI*1.0 endAngle:M_PI*1.5 clockwise:YES];
        
        [path addLineToPoint:CGPointMake(_arrowPoint.x-w2,_contentFrame.origin.y+0)];
        [path addLineToPoint:CGPointMake(_arrowPoint.x,_arrowPoint.y)];
        [path addLineToPoint:CGPointMake(_arrowPoint.x+w2,_contentFrame.origin.y+0)];
        
        [path addLineToPoint:CGPointMake(_contentFrame.origin.x+_contentFrame.size.width-_cornerRadius,_contentFrame.origin.y+0)];//_
        [path addArcWithCenter:CGPointMake(_contentFrame.origin.x+_contentFrame.size.width-_cornerRadius, _contentFrame.origin.y+_cornerRadius) radius:_cornerRadius startAngle:M_PI*1.5 endAngle:M_PI*2.0 clockwise:YES];
        [path addLineToPoint:CGPointMake(_contentFrame.origin.x+_contentFrame.size.width,_contentFrame.origin.y+_contentFrame.size.height-_cornerRadius)];
        [path addArcWithCenter:CGPointMake(_contentFrame.origin.x+_contentFrame.size.width-_cornerRadius, _contentFrame.origin.y+_contentFrame.size.height-_cornerRadius) radius:_cornerRadius startAngle:M_PI*0 endAngle:M_PI*0.5 clockwise:YES];
        [path addLineToPoint:CGPointMake(_contentFrame.origin.x+_cornerRadius,_contentFrame.origin.y+_contentFrame.size.height)];
        [path addArcWithCenter:CGPointMake(_contentFrame.origin.x+_cornerRadius, _contentFrame.origin.y+_contentFrame.size.height-_cornerRadius) radius:_cornerRadius startAngle:M_PI*0.5 endAngle:M_PI*1.0 clockwise:YES];
        
        [path closePath];
    }
    else if(_direction==BW_DIRECTION_DOWN)//下箭头
    {
        float h=_arrowPoint.y-(_contentFrame.origin.y+_contentFrame.size.height);//箭头三角形高度
        float w2=h*0.5;//箭头三角形半宽
        
        [path addArcWithCenter:CGPointMake(_contentFrame.origin.x+_cornerRadius, _contentFrame.origin.y+_cornerRadius) radius:_cornerRadius startAngle:M_PI*1.0 endAngle:M_PI*1.5 clockwise:YES];
        [path addLineToPoint:CGPointMake(_contentFrame.origin.x+_contentFrame.size.width-_cornerRadius,_contentFrame.origin.y+0)];
        [path addArcWithCenter:CGPointMake(_contentFrame.origin.x+_contentFrame.size.width-_cornerRadius, _contentFrame.origin.y+_cornerRadius) radius:_cornerRadius startAngle:M_PI*1.5 endAngle:M_PI*2.0 clockwise:YES];
        [path addLineToPoint:CGPointMake(_contentFrame.origin.x+_contentFrame.size.width,_contentFrame.origin.y+_contentFrame.size.height-_cornerRadius)];
        [path addArcWithCenter:CGPointMake(_contentFrame.origin.x+_contentFrame.size.width-_cornerRadius, _contentFrame.origin.y+_contentFrame.size.height-_cornerRadius) radius:_cornerRadius startAngle:M_PI*0 endAngle:M_PI*0.5 clockwise:YES];
        
        [path addLineToPoint:CGPointMake(_arrowPoint.x+w2,_contentFrame.origin.y+_contentFrame.size.height+0)];
        [path addLineToPoint:CGPointMake(_arrowPoint.x,_arrowPoint.y)];
        [path addLineToPoint:CGPointMake(_arrowPoint.x-w2,_contentFrame.origin.y+_contentFrame.size.height+0)];
        
        [path addLineToPoint:CGPointMake(_contentFrame.origin.x+_cornerRadius,_contentFrame.origin.y+_contentFrame.size.height)];
        [path addArcWithCenter:CGPointMake(_contentFrame.origin.x+_cornerRadius, _contentFrame.origin.y+_contentFrame.size.height-_cornerRadius) radius:_cornerRadius startAngle:M_PI*0.5 endAngle:M_PI*1.0 clockwise:YES];
        
        [path closePath];
    }
    else if(_direction==BW_DIRECTION_LEFT)//左箭头
    {
        float h=_contentFrame.origin.x-_arrowPoint.x;//箭头三角形高度
        float w2=h*0.5;//箭头三角形半宽
        
        [path addArcWithCenter:CGPointMake(_contentFrame.origin.x+_cornerRadius, _contentFrame.origin.y+_cornerRadius) radius:_cornerRadius startAngle:M_PI*1.0 endAngle:M_PI*1.5 clockwise:YES];
        [path addLineToPoint:CGPointMake(_contentFrame.origin.x+_contentFrame.size.width-_cornerRadius,_contentFrame.origin.y+0)];//_
        [path addArcWithCenter:CGPointMake(_contentFrame.origin.x+_contentFrame.size.width-_cornerRadius, _contentFrame.origin.y+_cornerRadius) radius:_cornerRadius startAngle:M_PI*1.5 endAngle:M_PI*2.0 clockwise:YES];
        [path addLineToPoint:CGPointMake(_contentFrame.origin.x+_contentFrame.size.width,_contentFrame.origin.y+_contentFrame.size.height-_cornerRadius)];
        [path addArcWithCenter:CGPointMake(_contentFrame.origin.x+_contentFrame.size.width-_cornerRadius, _contentFrame.origin.y+_contentFrame.size.height-_cornerRadius) radius:_cornerRadius startAngle:M_PI*0 endAngle:M_PI*0.5 clockwise:YES];
        [path addLineToPoint:CGPointMake(_contentFrame.origin.x+_cornerRadius,_contentFrame.origin.y+_contentFrame.size.height)];
        [path addArcWithCenter:CGPointMake(_contentFrame.origin.x+_cornerRadius, _contentFrame.origin.y+_contentFrame.size.height-_cornerRadius) radius:_cornerRadius startAngle:M_PI*0.5 endAngle:M_PI*1.0 clockwise:YES];
        
        [path addLineToPoint:CGPointMake(_contentFrame.origin.x,_arrowPoint.y+w2)];
        [path addLineToPoint:CGPointMake(_arrowPoint.x,_arrowPoint.y)];
        [path addLineToPoint:CGPointMake(_contentFrame.origin.x,_arrowPoint.y-w2)];
        
        [path closePath];
    }
    else if(_direction==BW_DIRECTION_RIGHT)//右箭头
    {
        float h=_arrowPoint.x-_contentFrame.origin.x-_contentFrame.size.width;//箭头三角形高度
        float w2=h*0.5;//箭头三角形半宽
        
        [path addArcWithCenter:CGPointMake(_contentFrame.origin.x+_cornerRadius, _contentFrame.origin.y+_cornerRadius) radius:_cornerRadius startAngle:M_PI*1.0 endAngle:M_PI*1.5 clockwise:YES];
        [path addLineToPoint:CGPointMake(_contentFrame.origin.x+_contentFrame.size.width-_cornerRadius,_contentFrame.origin.y+0)];//_
        [path addArcWithCenter:CGPointMake(_contentFrame.origin.x+_contentFrame.size.width-_cornerRadius, _contentFrame.origin.y+_cornerRadius) radius:_cornerRadius startAngle:M_PI*1.5 endAngle:M_PI*2.0 clockwise:YES];
        
        [path addLineToPoint:CGPointMake(_contentFrame.origin.x+_contentFrame.size.width,_arrowPoint.y-w2)];
        [path addLineToPoint:CGPointMake(_arrowPoint.x,_arrowPoint.y)];
        [path addLineToPoint:CGPointMake(_contentFrame.origin.x+_contentFrame.size.width,_arrowPoint.y+w2)];
        
        [path addLineToPoint:CGPointMake(_contentFrame.origin.x+_contentFrame.size.width,_contentFrame.origin.y+_contentFrame.size.height-_cornerRadius)];
        [path addArcWithCenter:CGPointMake(_contentFrame.origin.x+_contentFrame.size.width-_cornerRadius, _contentFrame.origin.y+_contentFrame.size.height-_cornerRadius) radius:_cornerRadius startAngle:M_PI*0 endAngle:M_PI*0.5 clockwise:YES];
        [path addLineToPoint:CGPointMake(_contentFrame.origin.x+_cornerRadius,_contentFrame.origin.y+_contentFrame.size.height)];
        [path addArcWithCenter:CGPointMake(_contentFrame.origin.x+_cornerRadius, _contentFrame.origin.y+_contentFrame.size.height-_cornerRadius) radius:_cornerRadius startAngle:M_PI*0.5 endAngle:M_PI*1.0 clockwise:YES];
        
        [path closePath];
    }
    else//无箭头
    {
        
        [path addArcWithCenter:CGPointMake(_contentFrame.origin.x+_cornerRadius, _contentFrame.origin.y+_cornerRadius) radius:_cornerRadius startAngle:M_PI*1.0 endAngle:M_PI*1.5 clockwise:YES];
        [path addLineToPoint:CGPointMake(_contentFrame.origin.x+_contentFrame.size.width-_cornerRadius,_contentFrame.origin.y+0)];
        [path addArcWithCenter:CGPointMake(_contentFrame.origin.x+_contentFrame.size.width-_cornerRadius, _contentFrame.origin.y+_cornerRadius) radius:_cornerRadius startAngle:M_PI*1.5 endAngle:M_PI*2.0 clockwise:YES];
        [path addLineToPoint:CGPointMake(_contentFrame.origin.x+_contentFrame.size.width,_contentFrame.origin.y+_contentFrame.size.height-_cornerRadius)];
        [path addArcWithCenter:CGPointMake(_contentFrame.origin.x+_contentFrame.size.width-_cornerRadius, _contentFrame.origin.y+_contentFrame.size.height-_cornerRadius) radius:_cornerRadius startAngle:M_PI*0 endAngle:M_PI*0.5 clockwise:YES];
        [path addLineToPoint:CGPointMake(_contentFrame.origin.x+_cornerRadius,_contentFrame.origin.y+_contentFrame.size.height)];
        [path addArcWithCenter:CGPointMake(_contentFrame.origin.x+_cornerRadius, _contentFrame.origin.y+_contentFrame.size.height-_cornerRadius) radius:_cornerRadius startAngle:M_PI*0.5 endAngle:M_PI*1.0 clockwise:YES];
        
        [path closePath];
        
        
    }
    
    _bubble.path=path.CGPath;
    _bubble.path=path.CGPath;
    [self.layer addSublayer:_bubble];
    [path release];
    
    
    
    //内容层（透明，作为容器）
    self.content=[[[UIView alloc] init] autorelease];
    _content.frame=_contentFrame;
    _content.backgroundColor=[UIColor clearColor];
    [self addSubview:_content];
    
    
    _isShown=NO;
}



//显示
- (void)show
{
    if(!_isShown)
    {
        _isShown=YES;
                
        _content.alpha=0.0f;
        _bubble.opacity=0.0f;
        _shadow.alpha=0.0f;
        
        self.frame=CGRectMake(0, 0, _screenSize.width, _screenSize.height);
        
        [UIView animateWithDuration:_animationDuration animations:^
        {
             _content.alpha=1.0f;
             _shadow.alpha=1.0f;
             _bubble.opacity=1.0f;
        }];
    
    }
}


//隐藏
- (void)hide
{
    if(_isShown)
    {
        _isShown=NO;
        
        [UIView animateWithDuration:_animationDuration animations:^
        {
             _content.alpha=1.0f;
             _shadow.alpha=1.0f;
             _bubble.opacity=1.0f;
        }
        completion:^(BOOL finished)
        {
             self.frame=CGRectMake(_screenSize.width, 0, _screenSize.width, _screenSize.height);
        }];
    }
    
}




- (void) dealloc
{
    [_shadow release];
    [_content release];
    [_bgColor release];
    [_borderColor release];
    [_shadowColor release];
    [_bubble release];
    
    [super dealloc];
}




@end
